package com.example.haojiutao.config;

import com.example.haojiutao.constant.Constants;
import com.example.haojiutao.interceptor.AuthLoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;


/**
 * @Description:
 * @Author: wang_meiying
 * @CreateTime: 2024/1/29 19:30
 */

@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
    //    跨域问题解决
//    @Value("${spring.mvc.allowed.origins}")
    @Value("${spring.mvc.allowed.origins}")
    private String[] allowedOriginPatterns;

//省略部分代码.....
    /**
     * @Description: 配置跨域策略
     * @param: registry
     **/
    @Autowired
    AuthLoginInterceptor authLoginInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
// registry.addMapping("/user/**").addMapping("/common/**");
        registry.addMapping("/**")
                .allowCredentials(true)/* allowCredentials 是否发送 Cookie */
                .allowedOrigins(allowedOriginPatterns) /* 放行那些原始域，
allowedOrigins 不可以包含 "*" */
//.allowedOriginPatterns(allowedOriginPatterns.toArray())
                .allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE"})
                .allowedHeaders("*")
                .allowedOriginPatterns("*")
                .exposedHeaders("*");
    }

    //注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration authRegistration = registry.addInterceptor(authLoginInterceptor);
        authRegistration.addPathPatterns("/user/**"); //拦截所有路径
        //拦截白名单，不需要登陆即可访问的 URL 例如：获取图片验证码、登录接口、注册接口、发送短信验证码接口

        authRegistration.excludePathPatterns("/user/register","/user/kaptcha","/user/smscode", "/user/login");

        /*authRegistration.excludePathPatterns("/user/register","/user/kaptcha","/user/smscode","/user/xianzhi",
                "/user/login","/user","/img/messageListPic/**","/img/avatorImages/**","/user/order/{orderNumber}"
                ,"/img/goodsPic/**","/user/fabu","/user/xianzhi/","/user/order/pay/{}","/img/goodsPic/**","/user/goods/{goodsId}"
                ,"/img/commentPic/**","/comment/{orderNumber}","/admin/user/user","/admin/user/{id}","/admin/user/info",
                "/img/","/img/**", "/user/info");*/
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img/swiper/**")
                .addResourceLocations(Constants.BANNER_PIC_PATH);

        /** 拦截商品图片请求并返回商品图片 */
        registry.addResourceHandler("/img/goodsListPic/**")
                .addResourceLocations(Constants.GOODSLIST_PIC_PATH);

        registry.addResourceHandler("/img/goodsPic/**")
                .addResourceLocations(Constants.GOODS_PIC_PATH);
        //上传文件
        /** 消息列表图片 */
        registry.addResourceHandler("/img/messageListPic/**")
                .addResourceLocations(Constants.MESSAGELIST_PIC_PATH);

        //上传头像
        registry.addResourceHandler("/img/**")   //拦截哪些请求地址，来返回静态资源, a/1.jpg
                .addResourceLocations(Constants.PIC_PATH);
        registry.addResourceHandler("/img/goodsPic/**")
                .addResourceLocations(Constants.GOODS_PIC_PATH);
        //上传闲置物品图片
//        registry.addResourceHandler("/img/**")
//                .addResourceLocations(Constants.PIC_PATH);
        //评论
        registry.addResourceHandler("/img/commentPic/**")
                .addResourceLocations(Constants.COMMENT_PIC_PATH);
        //商品页面轮播图
        registry.addResourceHandler("/img/goodsBanner/**")
                .addResourceLocations(Constants.GOODS_BANNER_PATH);



    }

}


